package com.jdrx.sw.sewage.service;

import com.google.common.collect.Maps;
import com.jdrx.platform.commons.rest.beans.vo.ResponseVO;
import com.jdrx.platform.commons.rest.exception.BizException;
import com.jdrx.sw.sewage.beans.entity.GatherDevPointPO;
import com.jdrx.sw.sewage.beans.vo.GatherDevVO;
import com.jdrx.sw.sewage.dao.GatherDevDAO;
import com.jdrx.sw.sewage.dao.GatherDevPointDAO;
import com.jdrx.sw.sewage.service.base.BaseServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@Service
public class GatherDevServiceImpl extends BaseServiceImpl {

    public static final Logger logger = LoggerFactory.getLogger(GatherDevServiceImpl.class);

    @Autowired
    private GatherDevDAO gatherDevDAO;
    @Autowired
    private GatherDevPointDAO gatherDevPointDAO;
    @Autowired
    private GDDataImportServiceImpl gatherDevDataImportService;
    @Autowired
    private GDDataCheckServiceImpl gDDataCheckService;

    public List<GatherDevVO> selectByStationId(Long stationId, Long processunitId) {
        return gatherDevDAO.selectByStationId(stationId, processunitId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Async("asyncServiceExecutor")
    public void handlePgVal(List<GatherDevPointPO> pgValList, String dataNode) throws BizException {
        if (CollectionUtils.isNotEmpty(pgValList)) batchUpdateByGatherDevPoiId(pgValList, dataNode);
    }

    private void batchUpdateByGatherDevPoiId(List<GatherDevPointPO> pgValList, String dataNode) throws BizException {
        int count;
        Map<String, Object> params = Maps.newHashMap();
        params.put("list", pgValList);
        params.put("dataNode", dataNode);
//        try {
        count = gatherDevPointDAO.batchUpdateByGatherDevPoiId(params);
//        } catch (Exception e) {
//            logger.error("批量插入失败", e);
//            throw new BizException("批量插入失败");
//        }
//        validateCount(count, pgValList.size(), "批量插入失败");
    }

    public ResponseVO collectionDataImport(MultipartFile file) throws IOException {
        return gatherDevDataImportService.collectionDataImport(file);
    }

    public ResponseVO collectionDataCheck(MultipartFile file) throws IOException {
        return gDDataCheckService.collectionDataCheck(file);
    }
}
